package com.example.textswitchtestdemo01.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.textswitchtestdemo01.R;
import com.example.textswitchtestdemo01.util.InitToolbar;

public class ThirdActivity extends AppCompatActivity {

    private GridView gv_totalcar_third;
    private GridView gv_redcar_third;
    private GridView gv_whitecar_third;
    private GridView gv_bluecar_third;
    int[] counts = {20, 7, 6, 7};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);
        InitToolbar.initToolbar(this, "第三页", true);
        initView();

        initData();

        initGrid();

    }

    private void initData() {

    }

    private void initGrid() {
        final BaseAdapter totalcarAdapter = new BaseAdapter() {
            String[] titles = {"总数(辆)", "蓝色汽车(辆)", "吉普汽车(辆)", "大众汽车(辆)"};

            class ViewHolder {
                public View rootView;
                public TextView tv_id;
                public TextView tv_count;

                public ViewHolder(View rootView) {
                    this.rootView = rootView;
                    this.tv_id = (TextView) rootView.findViewById(R.id.tv_id);
                    this.tv_count = (TextView) rootView.findViewById(R.id.tv_count);
                }

            }

            @Override
            public int getCount() {
                return counts.length;
            }

            @Override
            public Object getItem(int position) {
                return null;
            }

            @Override
            public long getItemId(int position) {
                return 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {

                View v;
                if (convertView == null) {
                    v = View.inflate(getApplicationContext(), R.layout.item_gv_totalcar, null);
                } else {
                    v = convertView;
                }
                ViewHolder viewHolder = new ViewHolder(v);
                viewHolder.tv_id.setText(titles[position]);
                viewHolder.tv_count.setText(String.valueOf(counts[position]));

                return v;
            }
        };
        gv_totalcar_third.setAdapter(totalcarAdapter);

        final BaseAdapter redcarAdapter = new BaseAdapter() {
            int count = 3;

            @Override
            public int getCount() {
                return count;
            }

            @Override
            public Object getItem(int position) {
                return null;
            }

            @Override
            public long getItemId(int position) {
                return 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View v;
                if (convertView == null) {
                    v = View.inflate(getApplicationContext(), R.layout.gv_item_car, null);
                } else {
                    v = convertView;
                }
                v.setOnClickListener(new View.OnClickListener() {
                    boolean flag = false;
                    @Override
                    public void onClick(View v) {
                        if (!flag){
                            flag = true;
                            v.setBackgroundColor(Color.RED);
                        }else {
                            flag = false;
                            v.setBackgroundColor(Color.WHITE);
                        }
                    }
                });
                return v;
            }
        };
        gv_redcar_third.setAdapter(redcarAdapter);

        final BaseAdapter bluecarAdapter = new BaseAdapter() {
            int count = 3;

            @Override
            public int getCount() {
                return count;
            }

            @Override
            public Object getItem(int position) {
                return null;
            }

            @Override
            public long getItemId(int position) {
                return 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View v;
                if (convertView == null) {
                    v = View.inflate(getApplicationContext(), R.layout.gv_item_car, null);
                } else {
                    v = convertView;
                }
                v.setOnClickListener(new View.OnClickListener() {
                    boolean flag = false;
                    @Override
                    public void onClick(View v) {
                        if (!flag){
                            flag = true;
                            v.setBackgroundColor(Color.RED);
                        }else {
                            flag = false;
                            v.setBackgroundColor(Color.WHITE);
                        }
                    }
                });
                return v;
            }
        };
        gv_bluecar_third.setAdapter(bluecarAdapter);
        final BaseAdapter whitecarAdapter = new BaseAdapter() {
            int count = 2;

            @Override
            public int getCount() {
                return count;
            }

            @Override
            public Object getItem(int position) {
                return null;
            }

            @Override
            public long getItemId(int position) {
                return 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {

                View v;
                if (convertView == null) {
                    v = View.inflate(getApplicationContext(), R.layout.gv_item_car, null);
                } else {
                    v = convertView;
                }

                v.setOnClickListener(new View.OnClickListener() {
                    boolean flag = false;
                    @Override
                    public void onClick(View v) {
                        if (!flag){
                            flag = true;
                            v.setBackgroundColor(Color.RED);
                        }else {
                            flag = false;
                            v.setBackgroundColor(Color.WHITE);
                        }
                    }
                });
                return v;
            }
        };
        gv_whitecar_third.setAdapter(whitecarAdapter);

//        gv_redcar_third.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
//            @Override
//            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//                redcarAdapter.notifyDataSetChanged();
//                totalcarAdapter.notifyDataSetChanged();
//                bluecarAdapter.notifyDataSetChanged();
//                whitecarAdapter.notifyDataSetChanged();
//            }
//
//            @Override
//            public void onNothingSelected(AdapterView<?> parent) {
//
//            }
//        });
    }

    private void initView() {
        gv_totalcar_third = (GridView) findViewById(R.id.gv_totalcar_third);
        gv_redcar_third = (GridView) findViewById(R.id.gv_redcar_third);
        gv_whitecar_third = (GridView) findViewById(R.id.gv_whitecar_third);
        gv_bluecar_third = (GridView) findViewById(R.id.gv_bluecar_third);
    }
}
